# coding: UTF-8
# Filename : medical_answer_manage.py
# Company : imedia
# Author : rose
# -*-coding:utf-8 -*-
from api.common.common import *
from api.common import mysql,log,utils,httputils,const,xml_tpl,err_tpl
import time
from MedicaAnswer import *

#插入咨询
def insert_ask(user_id, cus_id, content, reply):
    created_at = utils.getTimestamp()
    updated_at = created_at
    state = 1
    source = 1
    param = [cus_id, user_id, content, source, reply, created_at, updated_at, state]
    sql = '''insert  into `medical_answer`
    (`cus_id`,`user_id`,`content`,`source`,`reply`,`created_at`,`updated_at`,`state`) 
    values (%s,%s,%s,%s,%s,%s,%s,%s)
    '''
    rst = mysql.insert(sql, param)
    return rst

#获取咨询列表
def get_list(user_id, cus_id, size, pagenum):
    
    #获取ask单个元素
    source_dict = {1:'我', 2:'院方'}
    sql_ask = '''select id, source, content, created_at from medical_answer
        where user_id = %s and cus_id = %s and reply = 0 and state = 1'''
    param = [user_id, cus_id]
    ask_obj = mysql.get(sql_ask, param)
    if ask_obj == None: return None
    id = ask_obj[0]
    reply = ask_obj[0]
    source = ask_obj[1]
    content = ask_obj[2]
    created_at = ask_obj[3]
    ma = MedicaAnswer()
    ma.id = id
    ma.source = source
    ma.source_name = source_dict.get(source,'')
    ma.content = content
    ma.created_at = created_at
    ma.date = utils.getTimeFromTimetemp(created_at)
    ma.medicaAnswers = []
    #以下获取reply列表
    
    #不需要分页
    if int(size) == 0 or int(pagenum) == 0:
        sql_reply = '''select id, source, content, created_at from medical_answer
            where cus_id = %s and reply = %s and state = 1'''
        param = [cus_id, reply]
    #需要分页
    else:
        sql_reply = '''select id, source, content, created_at from medical_answer
            where cus_id = %s and reply = %s and state = 1
            limit %s, %s
            '''
        start_row = utils.get_star_row(size, pagenum)
        param = [cus_id, reply, start_row, int(size)]
        
    ask_objs = mysql.doSearch(sql_reply, param)
    if ask_objs == None: return ma
    for ask_obj in ask_objs:
        reply_ma = MedicaAnswer()
        id = ask_obj[0]
        source = ask_obj[1]
        content = ask_obj[2]
        created_at = ask_obj[3]
        reply_ma = MedicaAnswer()
        reply_ma.id = id
        reply_ma.source = source
        reply_ma.source_name = source_dict.get(source, '')
        reply_ma.content = content
        reply_ma.created_at = created_at
        reply_ma.date = utils.getTimeFromTimetemp(created_at)
        ma.medicaAnswers.append(reply_ma)
    return ma
    
def get_count(user_id, cus_id, reply_id):
    sql = '''select count(id) from medical_answer where cus_id = %s and reply = %s'''
    count_obj = mysql.get(sql, [cus_id, reply_id])
    count = 0
    if count_obj != None:
        count = count_obj[0]
    return count

def delete(obj_id, user_id, cus_id):
    sql = '''delete from medical_answer where id = %s and user_id = %s and cus_id = %s'''
    rst = mysql.deleteByParam(sql, [obj_id, user_id, cus_id])
    return rst
